package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.PubTaskPage;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.lab.LabChannel;
import com.ysd.lis.entity.lab.LabItemDict;
import com.ysd.lis.entity.lab.LabMaininfo;
import com.ysd.lis.mapper.lab.LabChannelMapper;
import com.ysd.lis.service.lab.LabChannelService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 通道号管理表 服务实现类
 * </p>
 *
 * @author Bai
 * @since 2024-01-26
 */
@Service
public class LabChannelServiceImpl extends ServiceImpl<LabChannelMapper, LabChannel> implements LabChannelService {

    @Autowired
    LabChannelMapper channelMapper;

    @Override
    public Result findChannelList(LabChannel labChannel) {
        if (StringUtils.isBlank(labChannel.getRptGroup())) {
            return Result.succ(new ArrayList<>());
        }
        MPJLambdaWrapper<LabChannel> mpjLambdaWrapper = new MPJLambdaWrapper();
        mpjLambdaWrapper.selectAll(LabChannel.class)
                .selectAs("Lid", LabItemDict::getItemNa, LabChannel::getItemDictName)
                .leftJoin(LabItemDict.class, "Lid", on -> on.eq(LabItemDict::getItemNo, LabChannel::getItemNo).eq(LabItemDict::getRptGroup, labChannel.getRptGroup()).eq(LabItemDict::getDelFlag, 0));
        SysUser user = RedisUserManager.getUser();
        if (ToolsUtils.isNotEmpty(user)) {
            mpjLambdaWrapper.eq(LabChannel::getOrgId, user.getOrgId());
        }
        mpjLambdaWrapper.eq(LabChannel::getRptGroup, labChannel.getRptGroup());
        mpjLambdaWrapper.eq(LabChannel::getInstrCode, labChannel.getInstrCode());
        mpjLambdaWrapper.eq(LabChannel::getItemNo, labChannel.getItemNo());
        mpjLambdaWrapper.eq(LabChannel::getDelFlag, 0).orderByAsc(LabChannel::getSeq);
        List<LabChannel> list = channelMapper.selectList(mpjLambdaWrapper);
        return Result.succ(list);
    }

    @Override
    public List<LabChannel> findChannelListByItemNoes(List<String> itemNoes, String instrCode, String rptGroup, String orgId) {
        MPJLambdaWrapper<LabChannel> mpjLambdaWrapper = new MPJLambdaWrapper();
        mpjLambdaWrapper.selectAll(LabChannel.class)
                .selectAs("Lid", LabItemDict::getItemNa, LabChannel::getItemDictName)
                .leftJoin(LabItemDict.class, "Lid", on -> on.eq(LabItemDict::getItemNo, LabChannel::getItemNo).eq(LabItemDict::getRptGroup, rptGroup).eq(LabItemDict::getDelFlag, 0));
        mpjLambdaWrapper.eq(StringUtils.isNotBlank(orgId), LabChannel::getOrgId, orgId);
        mpjLambdaWrapper.eq(LabChannel::getRptGroup, rptGroup);
        mpjLambdaWrapper.eq(LabChannel::getInstrCode, instrCode);
        mpjLambdaWrapper.in(LabChannel::getItemNo, itemNoes);
        mpjLambdaWrapper.eq(LabChannel::getDelFlag, 0);
        mpjLambdaWrapper.eq(LabChannel::getIsOpen, "1");
        mpjLambdaWrapper.orderByAsc(LabItemDict::getSeq);
        return channelMapper.selectList(mpjLambdaWrapper);
    }

    @Override
    public Result addLabChannel(LabChannel labChannel) {
        if (ToolsUtils.isEmpty(labChannel)) {
            return Result.fail("入参异常");
        }
        SysUser sysUser = RedisUserManager.getUser();
        labChannel.setOrgId(sysUser.getOrgId());
        Boolean save = this.save(labChannel);
        if (save) {
            return Result.succ(1, "添加成功！", null);
        } else {
            return Result.fail("添加失败！");
        }
    }

    @Override
    public Result editLabChannel(LabChannel labChannel) {

        if (ToolsUtils.isEmpty(labChannel)) {
            return Result.fail("入参异常");
        }

        Boolean save = this.updateById(labChannel);
        if (save) {
            return Result.succ(1, "修改成功！", null);
        } else {
            return Result.fail("修改失败！");
        }
    }

    @Override
    public Result editLabChannelOpen(LabChannel labChannel) {
        if (ToolsUtils.isEmpty(labChannel)) {
            return Result.fail("入参异常");
        }
        LambdaUpdateWrapper<LabChannel> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.set(LabChannel::getIsOpen, "0")
                .eq(LabChannel::getItemNo, labChannel.getItemNo())
                .eq(LabChannel::getInstrCode, labChannel.getInstrCode())
                .eq(LabChannel::getRptGroup, labChannel.getRptGroup())
                .eq(LabChannel::getOrgId, labChannel.getOrgId())
                .eq(LabChannel::getDelFlag, 0);

        channelMapper.update(null, queryWrapper);

        labChannel.setIsOpen("1");
        Boolean save = this.updateById(labChannel);
        if (save) {
            return Result.succ(1, "修改成功！", null);
        } else {
            return Result.fail("修改失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteChanne(LabChannel labChannel) {
        if (ToolsUtils.isEmpty(labChannel)) {
            return Result.fail("入参不可为空！");
        }
        //是否需要判断有没有相关业务正在使用这个规则，先走流程。此处需要确认
        int delete = channelMapper.deleteById(labChannel.getId());
        if (delete > 0) {
            return Result.succ(1, "删除成功！", null);
        } else {
            return Result.fail("删除失败！");
        }
    }


}
